package com.dormitory.management.system.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.entity.Student;
import com.dormitory.management.system.entity.StudentExcel;
import com.dormitory.management.system.req.*;
import com.dormitory.management.system.resp.StudentCheckInOutResp;
import com.dormitory.management.system.service.StudentService;
import com.dormitory.management.system.utils.ExcelUtil;
import com.dormitory.managemnet.util.core.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "学生信息管理")
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @ApiOperation("添加学生")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<String> save(@RequestBody StudentSaveReq req){
        return studentService.save(req);
    }

    @ApiOperation("删除学生")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<String> delete(
            @ApiParam(value = "学生id",required = true)
            @PathVariable Long id){
        if(!studentService.delete(id)) {
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("更新学生")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<String> update(@RequestBody StudentUpdateReq req){
        if(!studentService.update(req)) {
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }

    @ApiOperation("更新学生入/退住")
    @RequestMapping(value = "/checkInOut",method = RequestMethod.POST)
    public Result<String> checkInOut(@RequestBody StudentCheckInOutReq req){
        if(!studentService.checkInOut(req)) {
            return Result.error("操作失败！");
        }
        return Result.success("操作成功！");
    }

    @ApiOperation("学生分页列表")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<Student>> getPage(@RequestBody StudentPageReq req){
        return Result.ok(studentService.getPage(req));
    }

    @ApiOperation("学生入/退住分页列表")
    @RequestMapping(value = "/getCheckInOutPage",method = RequestMethod.POST)
    public Result<IPage<StudentCheckInOutResp>> getCheckInOutPage(@RequestBody StudentCheckInOutPageReq req){
        return Result.ok(studentService.getCheckInOutPage(req));
    }

    @ApiOperation("Excel导入学生")
    @PostMapping("/createByExcel")
    @ResponseBody
    public Result createStudent(String username, MultipartFile file)throws IOException {
        return studentService.importStudentByExcel(username,file);
    }

    /**
     * 下载Excel模板
     */

    @ApiOperation("Excel模板下载")
    @GetMapping("/excel/template")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入学生模板";
        String sheetName = "导入学生模板";
        List<StudentExcel> studentList = new ArrayList<>();
        studentList.add(new StudentExcel());
        try {
            ExcelUtil.writeExcel(response, studentList, fileName, sheetName, StudentExcel.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
